package com.mx.cofee;

import java.awt.Image;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Block extends JLabel{

	private boolean isFallingFinished;
	private String color;
	private ImageIcon image;
	public static int SIZE = 32;
	private int x, y;
	static String[] colors = new String[] { "blue", "green", "yellow", "pink",
			"red" };

	public Block(int x, int y) {
		setSize(32, 32);
		isFallingFinished = false;
		Random r = new Random();
		int rx = Math.abs(r.nextInt(4));
		color = colors[rx];
		this.x = x * Block.SIZE;
		this.y = y * Block.SIZE;
		image = new ImageIcon("src/com/mx/cofee/resource/" + color + ".png");
		setIcon(image);
		setVisible(true);
	}

	public Image getImage() {
		return image.getImage();
	}	
	
	public String changeColor(String newColor){
		String result = new String(color);
		color = newColor;
		image = new ImageIcon("src/com/mx/cofee/resource/" + color + ".png");
		setIcon(image);
		return result;
	}		
	
	public void stopFalling(){
		isFallingFinished = true;;
	}
	
	public boolean isFallingFinished(){
		return isFallingFinished;
	}	
	
	public String getColor(){
		return color;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void addY(int y) {
		this.y = this.y + y;
	}

	public void addX(int x) {
		this.x = this.x + x;
	}

	public String toString() {
		return x + "." + y;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((color == null) ? 0 : color.hashCode());
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Block other = (Block) obj;
		if (color == null) {
			if (other.color != null)
				return false;
		} else if (!color.equals(other.color))
			return false;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}	

	
}
